package dao;

import bean.Bill;
import bean.User;

import java.sql.ResultSet;
import java.util.ArrayList;

public class BillDao {
    private ArrayList getRet(ResultSet rs) throws Exception {
        ArrayList list = new ArrayList();
        while(rs.next()) {
            Bill ret = new Bill();
            ret.setUser_name(rs.getString("user_name"));
            ret.setDate(rs.getString("datetime"));
            ret.setAddr(rs.getString("addr"));
            ret.setMoney(rs.getDouble("money"));
            list.add(ret);
        }
        return list;
    }
    public boolean addBill(Bill bill) {
        DBbean db = new DBbean();
        db.getConnection();
        String sql = "insert into bill (user_name, datetime, addr, money) values (?, ?, ?, ?)";
        ArrayList params = new ArrayList();
        params.add(bill.getUser_name());
        params.add(bill.getDate());
        params.add(bill.getAddr());
        params.add(String.valueOf(bill.getMoney()));
        try {
            db.executeUpdate(sql, params);

        } catch(Exception e) {
            e.printStackTrace();
            db.close();
            return false;
        }
        db.close();
        return true;
    }
    // 根据时间从大到小获得所有表单
    public ArrayList getAllBills() {
        String sql = "select * from bill order by datetime desc ";
        DBbean db = new DBbean();
        db.getConnection();
        ResultSet rs  =  null;
        ArrayList bills = new ArrayList();
        try {
            rs = db.executeQuery(sql, null);
            bills = getRet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bills;
    }

    // 根据时间从大到小获得user_name的所有表单
    public ArrayList getAllBills(String user_name) {
        String sql = "select * from bill where user_name = (?) order by datetime desc";
        ArrayList list = new ArrayList();
        list.add(user_name);
        DBbean db = new DBbean();
        db.getConnection();
        ResultSet rs = null;
        ArrayList bills = new ArrayList();
        try {
            rs = db.executeQuery(sql, list);
            bills = getRet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bills;
    }
    public boolean delivergoods(String user_name) {
        DBbean db = new DBbean();
        db.getConnection();
        String sql = "update bill set isok = true where user_name =\"" + user_name + "\"";
        try {
            int i = db.executeUpdate(sql, null);
        } catch (Exception e) {
            e.printStackTrace();
            db.close();

            return false;
        }
        db.close();
        return true;
    }
    public ArrayList getnotoks() {
        String sql = "select * from bill where  isok=false order by datetime desc";
        DBbean db = new DBbean();
        db.getConnection();
        ResultSet rs = null;
        ArrayList bills = new ArrayList();
        try {
            rs = db.executeQuery(sql, null);
            bills = getRet(rs);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bills;
    }

}
